package order.service;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.SSEResponseModel;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.hunyuan.v20230901.HunyuanClient;
import com.tencentcloudapi.hunyuan.v20230901.models.ChatCompletionsRequest;
import com.tencentcloudapi.hunyuan.v20230901.models.ChatCompletionsResponse;
import com.tencentcloudapi.hunyuan.v20230901.models.Choice;
import com.tencentcloudapi.hunyuan.v20230901.models.Message;
import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult;
import lombok.AllArgsConstructor;
import lombok.Value;
import order.dto.UserNutrition;
import order.entity.PageResponse;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@AllArgsConstructor
@DubboService(version = "1.0.0")
public class OpenAiImpl implements OpenAiI {
    @DubboReference(version = "1.0.0")
    UserNutritionI userNutritionI;


   final   String SecretId = "AKIDGNxKSKyRkyynSt9E7nEJAnBJG3cw1td8";
    final String SecretKey = "bzHOGuJTO4VhdT6WigWDlATdbP5lRwCJ";
    final Credential cred = new Credential(SecretId, SecretKey);
    final ClientProfile clientProfile = new ClientProfile();
    // ap-guangzhou 响应服务器所在区 设置为广州，可以不写
    final private HunyuanClient  client = new HunyuanClient(cred, "ap-guangzhou", clientProfile);



    public String chat(String prompt) {
        try {
            ChatCompletionsRequest request = new ChatCompletionsRequest();
            //模型名
            request.setModel("hunyuan-standard");
            //开启流式响应，就是向chatgpt那样一点一点输出

            //问题信息
            Message[] messages = new Message[1];
            Message message = new Message();
            /*角色
            system - 指定对话场景
            user - 你问ai的
            assistant - ai 回答你的
            */
            request.setStream(false);
            message.setRole("user");
            //具体内容
            message.setContent(prompt + ",给出json格式为{\n" +
                    "  \"protein\": " +
                    "  \"fat\": " +
                    "  \"carbohydrates\": " +
                    "  \"fiber\": " +
                    "  \"energy\": " +
                    "}\n的protein,fats,carbohydrates,fiber和energy,只要一个数字,不要单位");
            messages[0] = message;
            request.setMessages(messages);
            ChatCompletionsResponse response = client.ChatCompletions(request);
            String result = AbstractModel.toJsonString(response);
            JSONObject jsonObject = new JSONObject(result);
            JSONArray choices = jsonObject.getJSONArray("Choices");
            JSONObject text = choices.getJSONObject(0).getJSONObject("Message");
            String content = text.getString("Content");
           // return content;
            // 正则表达式匹配JSON
            String regex = "\\{[^}]*\\}";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(content);
            if (matcher.find()) {
                String json = matcher.group();
                return matcher.group();
            } else {
                return content;
            }





        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return "error";
    }
public  String adviced(int userId){
    try {
        ChatCompletionsRequest request = new ChatCompletionsRequest();
        //模型名
        request.setModel("hunyuan-standard");
        //开启流式响应，就是向chatgpt那样一点一点输出

        //问题信息
        Message[] messages = new Message[1];
        Message message = new Message();
            /*角色
            system - 指定对话场景
            user - 你问ai的
            assistant - ai 回答你的
            */
        request.setStream(false);
        message.setRole("user");
        List<UserNutrition> userNutritions=userNutritionI.getUserNutrition(userId);
        //具体内容
        message.setContent("根据该用户的摄入的营养"+userNutritions+"给出一句建议,格式为:根据您近期营养,建议您");
        messages[0] = message;
        request.setMessages(messages);
        ChatCompletionsResponse response = client.ChatCompletions(request);
        String result = AbstractModel.toJsonString(response);
        JSONObject jsonObject = new JSONObject(result);
        JSONArray choices = jsonObject.getJSONArray("Choices");
        JSONObject text = choices.getJSONObject(0).getJSONObject("Message");
        String content = text.getString("Content");
        // return content;
        // 正则表达式匹配JSON
        String regex =  "\\\\[(.*?)\\\\]";
        Pattern pattern = Pattern.compile(regex);

        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {

            return matcher.group();
        } else {
            return content;
        }





    } catch (TencentCloudSDKException e) {
        e.printStackTrace();
    }
    return "error";

}

}